package de.dlrg.wms.gui.forms;

import java.util.HashMap;

import org.eclipse.swt.widgets.*;

import de.dlrg.wms.controller.controllers.SwimmerController;
import de.dlrg.wms.gui.forms.generic.GenericForm;
import de.dlrg.wms.model.AgeClass;
import de.dlrg.wms.model.Swimmer;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.layout.FormLayout;

public class SwimmerForm extends GenericForm {
	private Combo availableAgeClasses;
	private Swimmer swimmerToWorkOn;
	private Text txtName;
	private Text surname;
	private SwimmerController swimmerController;
	private HashMap<Integer, AgeClass> indexAgeClassMapping = new HashMap<Integer, AgeClass>();

	@Override
	protected void initData() {
		this.txtName.setText(this.swimmerToWorkOn.getName());
		this.surname.setText(this.swimmerToWorkOn.getSurName());

		int i = 0;
		for (AgeClass ageClass : this.swimmerController.getAgeClassController()
				.readAll()) {
			this.availableAgeClasses.add(ageClass.getName());
			this.indexAgeClassMapping.put(i, ageClass);
			if (ageClass.equals(this.swimmerToWorkOn.getAgeClass())) {
				this.availableAgeClasses.select(i);
				return;
			}
			i++;
		}
	}

	/**
	 * @wbp.parser.constructor
	 */
	public SwimmerForm(Composite parent, int style,
			final SwimmerController swimmerController) {
		this(parent, style, swimmerController, new Swimmer());
	}

	public SwimmerForm(Composite parent, int style,
			final SwimmerController swimmerController,
			final Swimmer swimmerToWorkOn) {
		super(parent, style);
		this.swimmerController = swimmerController;
		this.swimmerToWorkOn = swimmerToWorkOn;

		this.center.setLayout(new GridLayout(2, false));

		Label lblName = new Label(this.center, SWT.NONE);
		lblName.setText("Name");

		txtName = new Text(this.center, SWT.BORDER);
		txtName.setText("name");
		txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label lblSurname = new Label(this.center, SWT.NONE);
		lblSurname.setText("Surname");

		surname = new Text(this.center, SWT.BORDER);
		surname.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label lblAgeClass = new Label(this.center, SWT.NONE);
		lblAgeClass.setText("Age Class");

		availableAgeClasses = new Combo(this.center, SWT.NONE);
		availableAgeClasses.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				true, false, 1, 1));

		Composite composite = new Composite(this.center, SWT.NONE);
		composite.setLayout(new FormLayout());
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, false,
				false, 2, 1));


		this.initData();
	}

	@Override
	protected void okClicked() {
		try {
			swimmerToWorkOn.setName(txtName.getText());
			swimmerToWorkOn.setSurName(surname.getText());
			swimmerToWorkOn.setAgeClass(indexAgeClassMapping
					.get(availableAgeClasses.getSelectionIndex()));
			swimmerController.validate(swimmerToWorkOn);
			swimmerController.createOrUpdate(swimmerToWorkOn);
			
			close();
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

}
